package com.yix.jpademo.service.impl;

import com.yix.jpademo.dao.UserRepository;
import com.yix.jpademo.entity.User;
import com.yix.jpademo.service.TranceactionalTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TranceActionalTestService implements TranceactionalTestService {

    @Autowired
    private UserRepository userRepository;



    @Override
    public void updateUserName() {
        User user  = this.userRepository.getOne("4028811d762cd0b201762cd0b4f20000");
        User user1 = this.userRepository.getOne("4028811d762cda2c01762cda303e0000");
        user.setName("fri");
//        this.userRepository.updateUserNameById("fri111",user.getId());
        if(1==1){
            user1.setName("sec");
//            this.userRepository.updateUserNameById("sec222",user1.getId());
            throw new RuntimeException();
        }
    }

    @Transactional
    public void updateUser(){
        User user  = this.userRepository.getOne("4028811d762cd0b201762cd0b4f20000");
        User user1  = this.userRepository.getOne("4028811d762cda2c01762cda303e0000");
        user.setName("fri");
        this.userRepository.save(user);
        if(1==1){
            user1.setName("sec");
            this.userRepository.save(user1);
        }
    }
}
